<?php
include 'config.php';
function getLogicByName($name){
    global $db;
    $sql = "SELECT * FROM `t_config_logic`  WHERE `name` = '$name' ";
    $row = $db->getRow($sql);
    if ($row){
        $rs = json_decode($row['remark'],true);        
    }else {
        $rs = array();
    }
    return $rs[0];
}
$award= array(
    '1'=>array(
        'num'=>1, 
        'name'=>'星期一',#星期        
        'status'=>0,        #0可以领取，1，已经领取 2,不能领取
        'award'=>getLogicByName('day_1_gift'),
        'timeline'=>0
    ),
    '2'=>array(
        'num'=>2,
        'name'=>'星期二',     
        'status'=>0,        #0可以领取，1，已经领取 2,不能领取
        'award'=>getLogicByName('day_2_gift') ,
        'timeline'=>0    
    ),
    '3'=>array(
        'num'=>3,
        'name'=>'星期三',
        'status'=>0,        #0可以领取，1，已经领取 2,不能领取
        'award'=>getLogicByName('day_3_gift'),
        'timeline'=>0        
    ),
    '4'=>array(
        'num'=>4,
        'name'=>'星期四',
        'status'=>0,        #0可以领取，1，已经领取 2,不能领取
        'award'=>getLogicByName('day_4_gift') ,
        'timeline'=>0      
    ),
    '5'=>array(
        'num'=>5,
        'name'=>'星期五',
        'status'=>0,        #0可以领取，1，已经领取 2,不能领取
        'award'=>getLogicByName('day_5_gift'),
        'timeline'=>0      
    ),
    '6'=>array(
        'num'=>6,
        'name'=>'星期六',
        'status'=>0,        #0可以领取，1，已经领取 2,不能领取
        'award'=>getLogicByName('day_6_gift'),
        'timeline'=>0       
    ),
    '7'=>array(
        'num'=>7,
        'name'=>'星期日', 
        'status'=>0,        #0可以领取，1，已经领取 2,不能领取
        'award'=>getLogicByName('day_7_gift'),
        'timeline'=>0        
    ),
    '8'=>array(
        'num'=>8,
        'name'=>'全勤奖',      
        'status'=>0,        #0可以领取，1，已经领取 2,不能领取
        'award'=>getLogicByName('day_8_gift'),
        'timeline'=>0      
    ),
);
//print_r($award);die();
$uid = trim(@$_GET['uid']);
if (!$uid){
    die('-1');
}
$sql = "SELECT `day_award` FROM t_user_record WHERE uid = $uid ";
$row = $db->getRow($sql);
$nowWeek = date("w");
//echo $nowWeek;die();
if ($nowWeek==0) $nowWeek=7;

if (!$row){
    $myAward =  $award;    
}else {
    $json = $row['day_award'];
    $myAward = json_decode($json,true);
    if (!$myAward) {
        $myAward = $award;
    }    
}
$tmpAward = $myAward;
$tmpCountAward = 0;//这周登录的次数
foreach ($myAward as $k=>$v){
    if ($nowWeek==$v['num'] ){
        //echo $v['num'];
        if ($v['status']!=0){
            if ((time()-$v['timeline'])>(7*24*60*60)){//如果上次做任务的时间距离现在已经有一周了，则改状态 为可领取
                 $myAward[$k]['status']=0;
            }        
        }
    }
    if ($nowWeek<$v['num']){
        $myAward[$k]['status']=2;
    }
    if ($nowWeek>$v['num']){
        
        if ((time()-$v['timeline'])>(7*24*60*60)){//如果上次做任务的时间距离现在已经有一周了，则改状态 为不可领取
            $myAward[$k]['status']=2;
        }
        
    }
    
    if ($v['status']==1 ){
        $tmpCountAward++;
    }        
}
if ($tmpCountAward==7 and $nowWeek==7){
    foreach ($myAward as $k=>$v){
        if ($v['num']==8 ){
            $myAward[$k]['status']=0;
        }        
    }
}

       

//$db->query("UPDATE t_user_record SET day_award = '' ");
if ($tmpAward!=$myAward){   
    $sql = "UPDATE t_user_record SET day_award = '".mysql_real_escape_string(json_encode($myAward))."' WHERE uid = $uid ";
    $db->query($sql);
//    echo 'fffff';
}
foreach ($myAward as $k=>$v){
    //unset($myAward[$k]['timeline']);
}
//print_r($myAward);
$rs = json_encode($myAward);
echo $rs;
die();


?>